package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.xlhratingbar_lib.XLHRatingBar;
import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.SearchResultBean;
import com.scsj.supermarket.utils.Tool;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索结果列表
 */

public class SearchResultAdapter extends BaseQuickAdapter<SearchResultBean.DataBean, BaseViewHolder> {

    private Context mContext;
    //加载图片布局
    private LinearLayout scrollImgLayout;

    //所有数据的集合
    List<SearchResultBean.DataBean> allDataList = new ArrayList<>();

    //店铺内商品的集合
    List<SearchResultBean.DataBean.AppGoodsAndResourceVosBean> goodsList = new ArrayList<>();
    //我们需要一个集合来存放所有的商品Logo
    List<String> listImg = new ArrayList<>();
    //还需要一个集合来存放所有的商品Name
    List<String> goodsNames = new ArrayList<>();


    private String originPrice;
    private String nowPrice;

    public SearchResultAdapter(@Nullable List<SearchResultBean.DataBean> data, Context context) {

        super(R.layout.item_goods_list_base_layout1, data);
        this.allDataList = data;
        this.mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchResultBean.DataBean item) {

        scrollImgLayout = helper.getView(R.id.search_result_add_view_ll);
        /**
         * 开始为各个控件设置数据 let"s begin
         */
        //店铺名称
        TextView storeNameTv = helper.getView(R.id.index_name_tv);
        //已保标签
        TextView ispayBondTv = helper.getView(R.id.index_type1_tv);
        //店铺类型
        TextView storeTypeTv = helper.getView(R.id.index_type2_tv);
        //配送方式
        TextView deliveyMethodTv = helper.getView(R.id.index_type3_tv);

        //星星评级
        XLHRatingBar ratingBar = helper.getView(R.id.index_comm_rb);
        //月销量
        TextView salesNumTv = helper.getView(R.id.index_num_tv);
        //店铺活动描述
        TextView storeActiveContentTv = helper.getView(R.id.index_content_tv);

        //店铺头像
        ImageView index_icon_iv = helper.getView(R.id.index_icon_iv);
        if (item.getResourceLogoInfo() != null) {
            Glide.with(mContext)
                    .load(item.getResourceLogoInfo().getUrlFrameCapture())
                    .transform(new Tool.GlideRoundTransform(mContext, 15))
                    .placeholder(R.mipmap.default_good_ic)
                    .error(R.mipmap.default_good_ic)
                    .into(index_icon_iv);
        }
        //店铺名称
        storeNameTv.setText(item.getStore().getName());

        //保证金是这样判断的：：： 是否有交过保证金 1：是0：否
        if (item.getStore().getIsPayBond() == 1) {
            ispayBondTv.setVisibility(View.VISIBLE);
            ispayBondTv.setText("已保");
        } else if (item.getStore().getIsPayBond() == 0) {
            ispayBondTv.setVisibility(View.VISIBLE);
            ispayBondTv.setText("未保");
        } else {
            ispayBondTv.setVisibility(View.GONE);
        }

//店铺类型是这样的：：： 22天街网店 23实体店 24直营店铺
        if (item.getStore().getStoreType() != 0) {
            int storeType = item.getStore().getStoreType();
            Log.e("11", storeType + "666");
            switch (storeType) {
                case 22:
                    storeTypeTv.setText("天街网店");
                    break;
                case 23:
                    storeTypeTv.setText("实体店");
                    break;
                case 24:
                    storeTypeTv.setText("直营店铺");
                    break;
            }
        }

        //配送方式是这样的：：：配送方式 ：1.商家；2.商圈
        int deliveyMethod = item.getDeliveryBossType();
        switch (deliveyMethod) {
            case 1:
                deliveyMethodTv.setText("商家配送");
                break;
            case 2:
                deliveyMethodTv.setText("商圈配送");
                break;

        }

//这是店铺评分
        if (item.getStoreScore() != 0) {
            int starScore = item.getStoreScore();
            ratingBar.setCountSelected(starScore);
//            ratingBar.setNumStars(starScore);
        }

        //销量在这
        if (item.getStoreMonthlySales() != null) {
            String salesNum = item.getStoreMonthlySales();
            salesNumTv.setText("月销" + salesNum + "单");
        }

        //店铺活动了解一下
        //基础运费
        double storeMinDeliveryFee = item.getMinDeliveryFee();
        //距离
        String storeDiatance = item.getDistance();
        //起送价
        double originPrice = item.getStore().getOriginPrice();
        //超出该金额免运费，大于0才表示该值有效
        double freePrice = item.getStore().getFreePrice();
        StringBuilder conrenttv = new StringBuilder();
        if (storeMinDeliveryFee > 0.00001) {
            if (freePrice > 0.00001) {
                conrenttv.append("运费" + storeMinDeliveryFee + "元,超过" + freePrice + "元免运费,");
            } else {
                conrenttv.append("运费" + storeMinDeliveryFee + "元,");
            }
        } else {
            conrenttv.append("免运费,");
        }
        conrenttv.append(Tool.get2Money(originPrice) + "元起送,");
        conrenttv.append("距离" + Tool.get2Money(Double.valueOf(storeDiatance)) + "米");
        storeActiveContentTv.setText(conrenttv);


        /**
         * 下面是关于店铺内商品的信息
         */
        goodsList = item.getAppGoodsAndResourceVos();

//        listImg.clear();
//        goodsNames.clear();
        if (scrollImgLayout.getChildCount() > 0) {
            scrollImgLayout.removeAllViews();
        }
        List<SearchResultBean.DataBean.AppGoodsAndResourceVosBean> appGoodsAndResourceVos = item.getAppGoodsAndResourceVos();
        for (int i = 0; i < appGoodsAndResourceVos.size(); i++) {
            int width = (int) ((Tool.getwindowWidth(mContext) - Tool.dip2px(mContext, 20)) / 3.15);
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_compution_img_layout1, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    width, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(10, 0, Tool.dip2px(mContext, 10), 10);
            view.setLayoutParams(lp);
            view.setLayoutParams(lp);

            //设置商品图片
            ImageView reputationIv = view.findViewById(R.id.cover);
            if (appGoodsAndResourceVos.get(i).getResourceMasterInfo() != null) {
                Glide.with(mContext)
                        .load(appGoodsAndResourceVos.get(i).getResourceMasterInfo().getUrlFrameCapture())
                        .transform(new Tool.GlideRoundTransform(mContext, 15))
                        .placeholder(R.mipmap.default_good_ic)
                        .error(R.mipmap.default_good_ic)
                        .into(reputationIv);
            }
            //商品名称
            ((TextView) view.findViewById(R.id.name)).setText(appGoodsAndResourceVos.get(i).getGoods().getGoodsName());

            if (Tool.get2Money(appGoodsAndResourceVos.get(i).getGoods().getGoodsPrice()).equals(Tool.get2Money(appGoodsAndResourceVos.get(i).getGoods().getGoodsPromotionPrice()))) {
                ((TextView) view.findViewById(R.id.goodsPromotionPrice_tv)).setText(appGoodsAndResourceVos.get(i).getGoods().getGoodsPromotionPrice() + "");
            } else {
                //商品销售价格(原价)  goodsPrice
                ((TextView) view.findViewById(R.id.goodsPrice_tv)).setText(appGoodsAndResourceVos.get(i).getGoods().getGoodsPrice() + "");
                //商品促销价格(售价) goodsPromotionPrice
                ((TextView) view.findViewById(R.id.goodsPromotionPrice_tv)).setText(appGoodsAndResourceVos.get(i).getGoods().getGoodsPromotionPrice() + "");
            }

            scrollImgLayout.addView(view);
        }
//        for (int b = 0; b < goodsList.size(); b++) {
//            //又是一个循环操作
//            String goodsLogo = (String) goodsList.get(b).getGoods().getGoodsImage();
//            listImg.add(goodsLogo);
//            //设置商品名字
//            String goodsName = goodsList.get(b).getGoods().getGoodsName();
//            goodsNames.add(goodsName);
//
//            //获取商品原价
//            this.originPrice = String.valueOf(goodsList.get(b).getGoods().getGoodsPrice());
//            //获取商品现价
//            nowPrice = String.valueOf(goodsList.get(b).getGoods().getGoodsPromotionPrice());
//        }
//
//        /**
//         * 添加轮播图片
//         */
//        addScrollImg(listImg, goodsNames, this.originPrice, nowPrice);

    }

    private void addScrollImg(List<String> img, List<String> listImg, String OriginPrice, String nowPrice) {

        if (scrollImgLayout.getChildCount() > 0) {
            scrollImgLayout.removeAllViews();
        }
        for (int i = 0; i < listImg.size(); i++) {

            final int index2 = i;
            int width = (int) ((Tool.getwindowWidth(mContext) - Tool.dip2px(mContext, 20)) / 3.15);
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_compution_img_layout1, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    width, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(10, 0, Tool.dip2px(mContext, 10), 10);
            view.setLayoutParams(lp);
            view.setLayoutParams(lp);

            //设置商品图片
            ImageView reputationIv = view.findViewById(R.id.cover);
            Glide
                    .with(mContext)
                    .load(listImg.get(i))
                    .transform(new Tool.GlideRoundTransform(mContext, 15))
                    .placeholder(R.mipmap.default_good_ic)
                    .error(R.mipmap.default_good_ic)
                    .into(reputationIv);
            TextView goodName = view.findViewById(R.id.name);
            scrollImgLayout.addView(view);
//            //设置现价
//            TextView nowPriceTv = view.findViewById(R.id.money_tv);
//            nowPriceTv.setText(nowPrice);
//            //设置原价
//            TextView originPriceTv = view.findViewById(R.id.money_tv);
//            originPriceTv.setText(originPrice);
        }
    }
}
